// pages/Search/Search.js
const App = getApp()
const server = App.path.Server

Page({

    /**
     * 页面的初始数据
     */
    data: {
        selectorDefaultData: {
            community: {
                array: []
            }
        },
        selectedData: {
            community: null,
            status: null
        },
        status: [{ name: '全部', uid: '' }, { name: '已到访', uid: 1 }, { name: '未到访', uid: 0 }],
        idOrName: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getCommunityList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        wx.hideLoading()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    // Selector回调
    _setSelectedData(e) {
        let { key, value } = e.detail;
        // console.log(value)
        let temp = this.data.selectedData
        temp[key] = value.uid
        this.setData({
            selectedData: temp
        })
        // console.log(`key is:${key}, value is: ${value.uid}`)
    },

    // input输入监控
    inputChangeIdOrName(e) {
        // console.log(e)
        this.setData({
            idOrName: e.detail.value
        })
    },

    // 扫描二维码
    scanCode(e) {
        App.scanCode(this)
    },

    // 获取社区名
    getCommunityList() {
        let _this = this
        wx.request({
            url: server + '/v1/community/getAllCommunity',
            method: 'POST',
            header: {
              "Content-Type": "application/x-www-form-urlencoded",
              'Authorization': 'Bearer ' + App.globalData.token.access_token
            },
            complete(res) {
                App.judgeCode(res, function(_res){
                    let temp = _this.data.selectorDefaultData
                    temp.community.array = _res.data
                    _this.setData({
                        selectorDefaultData: temp
                    })
                })
            }
        })
    },

    // 点击查找按钮
    tapSearchButton (){
        let _this = this
        let temp = _this.data.selectedData
        temp['idOrName'] = _this.data.idOrName
        wx.redirectTo({
            url: App.path.Old + '?search=ture&order=' + JSON.stringify(temp),
        })
    },
})